import com.baidu.echarts.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet("/demo")
public class DemoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type", "text/json;charset=utf-8");
//        resp.getWriter().write("{\"series\":{\"name\": \"访问来源\",\"type\": \"pie\",\"radius\": \"55%\",\"center\": [\"50%\", \"60%\"],\"data\": [{\"value\": 335,\"name\": \"直接访问\"},{\"value\": 310,\"name\": \"邮件营销\"},{\"value\": 234,\"name\": \"联盟广告\"},{\"value\": 135,\"name\": \"视频广告\"},{\"value\": 1548,\"name\": \"搜索引擎\"}]},\"legend\":[\"直接访问\", \"邮件营销\", \"联盟广告\", \"视频广告\", \"搜索引擎\"]}");
        resp.getWriter().write(testEcharts());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type", "text/json;charset=utf-8");
        resp.getWriter().write("{\"series\":{\"name\": \"访问来源\",\"type\": \"pie\",\"radius\": \"55%\",\"center\": [\"50%\", \"60%\"],\"data\": [{\"value\": 233,\"name\": \"直接访问N\"},{\"value\": 122,\"name\": \"邮件营销N\"},{\"value\": 54,\"name\": \"联盟广告N\"},{\"value\": 388,\"name\": \"视频广告N\"},{\"value\": 2112,\"name\": \"搜索引擎N\"}]},\"legend\":[\"直接访问N\", \"邮件营销N\", \"联盟广告N\", \"视频广告N\", \"搜索引擎N\"]}");

    }

    private String testEcharts() throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        Option option = new Option();


        Title title = new Title();
        title.setText("图说API标题测试");
        title.setSubtext("图说API子标题");
        title.setX("center");
        option.setTitle(title);

        Tooltip tooltip = new Tooltip();
        tooltip.setTrigger("item");
        tooltip.setFormatter("{a} <br/>{b} : {c} ({d}%)");
        option.setTooltip(tooltip);

        Legend legend = new Legend();
        legend.setOrient("vertical");
        legend.setData(Arrays.asList(new String[]{"直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"}));
        legend.setX("left");
        option.setLegend(legend);

        option.setCalculable(true);

        Series series = new Series();
        series.setName("访问来源");
        series.setCenter(Arrays.asList(new String[]{"50%", "60%"}));
        series.setRadius("50%");
        series.setType("pie");


        List<Series> seriesList = new ArrayList<>();
        List<SeriesData> data = new ArrayList<>();
        SeriesData data1 = new SeriesData();
        data1.setName("直接访问");
        data1.setValue("234");
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("邮件营销");
        data1.setValue("234");
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("联盟广告");
        data1.setValue("234");
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("视频广告");
        data1.setValue("234");
        data.add(data1);

        data1 = new SeriesData();
        data1.setName("搜索引擎");
        data1.setValue("234");
        data.add(data1);

        series.setData(data);
        seriesList.add(series);
        option.setSeries(seriesList);

        Toolbox toolbox = new Toolbox();
        toolbox.setShow(true);
        Feature feature = new Feature();
        feature.setMark(new MarkItem(true));
        feature.setRestore(new MarkItem(true));
        feature.setSaveAsImag(new MarkItem(true));
        feature.setDataView(new DataView(true, true));

        toolbox.setFeature(feature);
        option.setToolbox(toolbox);


        System.out.println(mapper.writeValueAsString(option));
        return mapper.writeValueAsString(option);
    }
}
